// pages/home/home.js

import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'

import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // showPrivacy: false,
        privacyContractName:'123123',
        phone: '',
        isLogin: wx.getStorageSync('isLogin') ? wx.getStorageSync('isLogin') : false,
        showModal: false,
        isChooseProtocal: false,
        warnToast: false,
        hideWarnToast: false,
        // 登录协议, 数据板块
        protocal_title: '',
        protocal_con: '',
        protocal_company: '',
        is_choose_xieyi: 'false',
        imagesrc:'',
        loginsrc:'',
        theme:'',
        aitext:''
    },
    openPrivacyContract() {
        const _ = this
        wx.openPrivacyContract({
            fail: () => {
                wx.showToast({
                    title: '遇到错误',
                    icon: 'error'
                })
            }
        })
    },
    // 拒绝隐私协议
    exitMiniProgram() {
        // 直接退出小程序
        wx.exitMiniProgram()
    },
    // 同意隐私协议
    handleAgreePrivacyAuthorization() {
        const _ = this
        _.setData({
            showModal: false,
            is_choose_xieyi: 'true',
            isChooseProtocal: true
        });
       
    },
    /**
     * 新版本,需要将用户的手机信息也进行缓存
     */
    onLoad() {
        // wx.onNeedPrivacyAuthorization((resolve, eventInfo) => {
        //     console.log(resolve);
        //     console.log( eventInfo);
        //     // 需要用户同意隐私授权时
        //     // 弹出开发者自定义的隐私授权弹窗
        //     this.setData({
        //         showModal: true
        //     })
        //     this.resolvePrivacyAuthorization = resolve
        //   })
        wx.showShareMenu({withShareTicket: true,menus: ['shareAppMessage', 'shareTimeline']});
    },
    onShareAppMessage() {
        return {title: '天纪云',path: '/pages/login/index'}
    },
    onShareTimeline() {
        return {title: '天纪云',path: '/pages/login/index'}
    },
    /**
     * 点击进入,事件操作
     */
    toIndex() {
        if (this.data.isChooseProtocal) {
            // 勾选协议后, 也需要判断. 是佛有授权手机
            var pages = getCurrentPages()
            if(pages.length>0){
                wx.navigateBack({
                  delta: 1,
                })
                let toUrl =  wx.getStorageSync('toMenu')?wx.getStorageSync('toMenu'):'/pages/menu/index';
                console.log('url='+toUrl)
                wx.redirectTo({
                    url: toUrl,
                })
                wx.setStorageSync('isChooseProtocal',true);
                wx.setStorageSync('isLogin',true);
                
            } else {
                wx.navigateTo({
                    url: '/pages/menu/index',
                })
            }
        } else {
            wx.showModal({
                title: '温馨提示',
                showCancel: false,
                content: '请勾选用户协议',
                success(res) {

                }
            })
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if(wx.getStorageSync('theme') == 'style1'){
            this.setData({
                imagesrc: 'https://aiw.oss-cn-beijing.aliyuncs.com/year/Group17.png',
                loginsrc:'https://aiw.oss-cn-beijing.aliyuncs.com/logo.png',
                theme:'style1',
                aitext:'../../images/aizhinengtuan.png'
            })
        }else{
            this.setData({
                imagesrc: 'https://aiw.oss-cn-beijing.aliyuncs.com/v3/su_an@2x.png',
                loginsrc:'https://aiw.oss-cn-beijing.aliyuncs.com/logo.png',
                theme:'init',
                aitext:'../../images/WechatIMG86.png'

            })
        }
        // console.log('----s',wx.getStorageSync('theme'));

        let cachePhoneFlag = wx.getStorageSync('user_phone');
        if (!cachePhoneFlag.phoneNumber) {
            wx.setStorageSync('isLogin', false);
            this.setData({
                isLogin: false
            });
        } else {
            this.setData({
                isLogin: true
            })
        }

        setTimeout(() => {
            app.show(this, 'show', 1)
            app.show(this, 'showbtn', 1, 2000)
        }, 1000);
        // 获取用户登录协议
        var that = this;
        wx.request({
            url: app.globalData.domain + 'index/index/getProtocal',
            data: {},
            success: function (res) {
                if (!res.data.code) {
                    that.setData({
                        protocal_title: res.data.data.title,
                        protocal_con: res.data.data.content,
                        protocal_company: res.data.data.company
                    });
                }

            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        setTimeout(() => {
            app.show(this, 'show', 0)
        }, 1000);
    },

    
    /**
     * 弹出用户登录协议
     */
    onClickShow() {
        this.setData({
            showModal: true
        });
    },

    onClickHide() {
        this.setData({
            showModal: false
        });
    },
    /**
     * 勾选协议
     */
    checkboxChange: function (e) {
        var checkedVal = e.detail.value;
        var oldChooseData = checkedVal.length ? true : false;

        this.setData({
            isChooseProtocal: oldChooseData
        });
    },

    /**
     * 勾选协议
     */
    changeImg: function (e) {
        console.log('debug::', e);
        let chooseRes = e.currentTarget.dataset.choosed == 'false' ? false : true;
        console.log('是否选中::', chooseRes);
        this.setData({
            is_choose_xieyi: e.currentTarget.dataset.choosed,
            isChooseProtocal: chooseRes
        });
    }
})